/**
 * Created by Administrator on 2016/7/2.
 */
var user_token =  sessionStorage.user_token;
//60秒重新发送
var countdown=60;
function settime(obj) {
    if (countdown == 0) {
        obj.removeAttribute("disabled");
        obj.innerHTML="获取验证码";
        return;
    } else {
        obj.setAttribute("disabled", true);
        obj.innerHTML="重新发送(" + countdown + ")";
        countdown--;
    }
    setTimeout(function() {
            settime(obj) }
        ,1000)
}


$("document").ready(function(){
    $(".procedure1 button").click(function(){
        if($(".procedure1 input").val() == 0){
            createWindow("请输入手机号");
            function createWindow(msg) {
                var window =$("<section class='showMsg'></section>");
                var show = "<p>"+msg+"</p>";
                window.append(show);
                $("body").append(window);
                setTimeout("$('.showMsg').remove()",3000);
            }
        }else{
            $(".procedure1").css("display","none");
            $(".procedure2").css("display","block");
            var mobile = $(".procedure1 input").val();
            $.post(commonval+"/sendForPwd",{
                mobile:mobile,
                device:"wap",
                type:"api_sendpwd"
            },function(data){
                if(data.status ==1){
                    createWindow("验证码发送成功");
                    function createWindow(msg) {
                        var window =$("<section class='showMsg'></section>");
                        var show = "<p>"+msg+"</p>";
                        window.append(show);
                        $("body").append(window);
                        setTimeout("$('.showMsg').remove()",3000);
                    }
                    $("#securityCode").css("display","inherit");
                    $("#securityCode").click();
                    $("#securityCode").click(function(){
                        var mobile = $(".procedure1 input").val();
                        $.post(commonval+"/sendForPwd",{
                            mobile:mobile,
                            token:token,
                            type:"api_sendpwd"
                        },function(data){
                            if(data.status ==1) {
                                createWindow("重新验证码发送成功");
                                function createWindow(msg) {
                                    var window = $("<section class='showMsg'></section>");
                                    var show = "<p>" + msg + "</p>";
                                    window.append(show);
                                    $("body").append(window);
                                    setTimeout("$('.showMsg').remove()", 3000);
                                }
                            }else{
                                createWindow(data.msg);
                                function createWindow(msg) {
                                    var window =$("<section class='showMsg'></section>");
                                    var show = "<p>"+msg+"</p>";
                                    window.append(show);
                                    $("body").append(window);
                                    setTimeout("$('.showMsg').remove()",3000);
                                }
                            }
                        });
                    });
                }else{
                    createWindow(data.msg);
                    function createWindow(msg) {
                        var window =$("<section class='showMsg'></section>");
                        var show = "<p>"+msg+"</p>";
                        window.append(show);
                        $("body").append(window);
                        setTimeout("$('.showMsg').remove()",3000);
                    }
                }
            });
        }
    });
    //checkCode
    $(".procedure2 button:nth-child(3)").click(function(){
        if($(".procedure2 input").val() == 0){
            createWindow("请输入验证码");
            function createWindow(msg) {
                var window =$("<section class='showMsg'></section>");
                var show = "<p>"+msg+"</p>";
                window.append(show);
                $("body").append(window);
                setTimeout("$('.showMsg').remove()",3000);
            }
        }else{
            $(".procedure2").css("display","none");
            $(".procedure3").css("display","block");
            $("header p").text("更新密码");
        }
    });

    $(".procedure3 button").click(function(){
        if($(".procedure3 input").val() == 0){
            createWindow("新密码");
            function createWindow(msg) {
                var window =$("<section class='showMsg'></section>");
                var show = "<p>"+msg+"</p>";
                window.append(show);
                $("body").append(window);
                setTimeout("$('.showMsg').remove()",3000);
            }
        }else{
            var code = $(".procedure2 input").val();
            var password = $(".procedure3 input").val();
            var mobile = $(".procedure1 input").val();
            $.post(commonval+"/updatepwd",{
                mobile:mobile,
                device:"wap",
                code:code,
                newpassword:password
            },function(data){
                if(data.status == 1){
                    function createWindow(msg) {
                        var window =$("<section class='showMsg'></section>");
                        var show = "<p>"+msg+"</p>";
                        window.append(show);
                        $("body").append(window);
                        setTimeout("$('.showMsg').remove()",3000);
                    }
                    history.go(-1);
                }else{
                    createWindow(data.msg);
                    function createWindow(msg) {
                        var window =$("<section class='showMsg'></section>");
                        var show = "<p>"+msg+"</p>";
                        window.append(show);
                        $("body").append(window);
                        setTimeout("$('.showMsg').remove()",3000);
                    }
                }

            });

        }
    });
});